/*
 *  Copyright (C) 2006 Takeharu KATO
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if !defined(G2IPMSG_PUBCRYPT_H)
#define G2IPMSG_PUBCRYPT_H
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#if defined(USE_OPENSSL)
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

#define RSA_KEYGEN_RETRY    10
#define RSA_KEY_INDEX_2048   0
#define RSA_KEY_INDEX_1024   1
#define RSA_KEY_INDEX_512    2
#define RSA_KEY_MAX          3
#define RSA_CAPS            (IPMSG_RSA_512|IPMSG_RSA_1024|IPMSG_RSA_2048)
#define SIGN_CAPS           (IPMSG_SIGN_MD5|IPMSG_SIGN_SHA1)

#define MD5_DIGEST_LEN       16
#define SHA1_DIGEST_LEN      20
#define MD5SHA1_DIGEST_LEN   36

#define RSA_DIR_INVALID_FLAGS (S_IWOTH|S_IWGRP) 
#define RSA_PRIVKEY_FNAME_PREFIX "priv-key-"
#define RSA_PUBKEY_FNAME_PREFIX  "pub-key-"

int pcrypt_get_rsa_key_length(unsigned long key_type,size_t *len);
int pcrypt_crypt_parse_anspubkey(const char *string,unsigned long *peer_cap,unsigned char **crypt_e,unsigned char **crypt_n);
int pcrypt_crypt_generate_anspubkey_string(unsigned long cap,const char **message_p);
int pcrypt_crypt_generate_getpubkey_string(unsigned long cap,const char **message_p);
int pcrypt_crypt_init_keys(void);
int pcrypt_crypt_release_keys(void);
int pcrypt_crypt_refer_rsa_key_with_index(int index,RSA **rsa_p);
int pcrypt_crypt_refer_rsa_key(unsigned long cap,RSA **rsa_p);
int pcrypt_encrypt_message(const unsigned long peer_cap,const char *peer_e,const char *peer_n,const char *plain,size_t plain_length,char **ret_buff,unsigned long *key_type);
int pcrypt_decrypt_message(unsigned long cap,const char *encrypted_skey,char **ret_buff,size_t *decrypted_len);
int pcrypt_sign(unsigned long cap,unsigned long sign_type,unsigned char *message, unsigned char **ret_buff);
int pcrypt_verify_sign(unsigned long cap,unsigned long sign_type,unsigned char *msg_str, 
		       unsigned char *sign,unsigned char *peer_e,unsigned char *peer_n);
int pcrypt_store_rsa_key(unsigned long key_name,pem_password_cb *cb);
int pcrypt_load_rsa_key(unsigned long key_name,pem_password_cb *cb);
int select_asymmetric_key(unsigned long peer_cap,RSA *rsa,unsigned long *selected_key);
int select_signature(unsigned long peer_cap,unsigned long *selected_algo,int speed);
#else
#define RSA_CAPS            (0)
#define SIGN_CAPS           (0)
#endif /*  USE_OPENSSL  */
#endif  /*  G2IPMSG_PUBCRYPT_H  */
